package com.example.pungolo.journal;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Calendar;
import java.util.Date;

import com.example.pungolo.journal.model.DiaryDatabase;
import com.example.pungolo.journal.model.DiaryEntry;
import com.example.pungolo.journal.model.DiaryIO;
import com.example.pungolo.journal.model.DiaryPage;
import com.example.pungolo.journal.model.Mood;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class DebugToolsActivity extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_debug_tools);
    final Context context  = this.getApplicationContext();
    
    final Button dbgbtnSmpDb = (Button) findViewById(R.id.dbgbtn_create_sample_database);
    final Button dbgbtnDelDb = (Button) findViewById(R.id.dbgbtn_delete_database);
    
    dbgbtnDelDb.setOnClickListener(new OnClickListener() {public void onClick(View v) {
      DiaryIO.resetDb();
      finish();
    }});
    
    dbgbtnSmpDb.setOnClickListener(new OnClickListener() {public void onClick(View v) {
      DiaryIO.resetDb();
      DiaryDatabase db = DiaryIO.getDatabase();
      
      final long msInADay = 24*60*60*1000;
      final long now = Calendar.getInstance().getTime().getTime();
      final File dir = DiaryIO.getDiaryDirectory();
      final Resources res = context.getResources();
      
      int[] raws = {
          R.raw.gattodarth, R.raw.gattomessicano, R.raw.gattopalestrato, R.raw.gattopipi, R.raw.gattoritardato,R.raw.gattoshopper,R.raw.gattosimpatico,
          R.raw.gattopoker,R.raw.gattotv, R.raw.gattopoop,R.raw.gattopillole,R.raw.gatticappello,R.raw.gattospazzolino,R.raw.gattogesu,
          R.raw.gattotouchpad
          };
      
      int d = 0;
      for (int i = 0; i < raws.length; i++) {
        File file = new File(dir, "cat" + (i+1) + ".jpg");
        dumpStream(res.openRawResource(raws[i]), file);
        DiaryPage page = new DiaryPage();
        page.setDate(new Date(now - d * msInADay));
        page.setImagePath(file.getPath());
        int entryCount = (int)(Math.random() * 15);
        for (int j = 0; j < entryCount; j++) {
          DiaryEntry entry = new DiaryEntry();
          entry.setMood(Mood.uncertain);
          entry.setText("TEMEF " + j);
          entry.setDate(new Date(page.getDate().getTime() + j * 1000));
          page.addEntry(entry);
        }
        db.saveDiaryPage(page);
        
        d += 1 + (int)(Math.random() * 2);
      }
      finish();
    }});
    
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.debug_tools, menu);
    return true;
  }

  private static boolean dumpStream(InputStream in, File file) {
    FileOutputStream out = null;
    try {
      out = new FileOutputStream(file);
      int read = 0;
      byte[] bytes = new byte[1024];
   
      while ((read = in.read(bytes)) != -1) {
        out.write(bytes, 0, read);
      }
    } catch (IOException e) {
      return false;
    } finally {
      if (out != null)
        try {
          out.close();
        } catch (IOException e) { }
    }
    return true;
  }

}
